import pygame
import battleScreen
#import pokemon
#import move
#import button



battleScreenHeight = 144
battleScreenWidth  = 256

screen = pygame.display.set_mode([512, 288])
battlescreen = pygame.Surface([battleScreenWidth, battleScreenHeight])

pygame.init()
                                            ## NEED TO FIX FIREBLAST
ATTACK = 9                                  ##  #4 when the player attacks, blit player's pokemon after the attack animation
                                            ##     same thing when attack = 10   and 12
BLACK = [0, 0, 0]
WHITE = [255, 255, 255]

animationFrames = [10, 17, 23, 10, 30, 17, 17, 11, 14, 11, 11, 4, 13, 4]
animationHeight = [144, 191, 191, 144, 144, 191, 144, 191, 191, 144, 191, 191, 191, 32]
animationHeightv2 = [144, 144, 144, 144, 144, 144, 144, 144, 144, 144, 144, 144, 144, 32]
animationWidth = [256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 32]
animationRow = [10, 17, 23, 4, 30, 17, 17, 11, 14, 11, 11, 4, 13, 4]
animationCollumn = [1, 1, 1, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
############       0                1                 2              3          4            5             6           7             8                 9               10              11             12          13
animation = ['FIRE-BLAST-1', 'HYDRO-CANNON-2', 'SHADOW-BALL-1', 'Seed-Flare', 'SURFV2', 'MUD-SHOT-1', 'AEROBLAST-1', 'SWIFT-1', 'HYDRO-PUMP-1', 'FRENZY-PLANT-1', 'BLAST-BURN-1', 'WOOD-HAMMER-1', 'MUDSPORT', 'THUNDAR']
animationBackground = ['fireblastybackground', 'Hydro-cannon-back', 'whiteBackground', 'whiteBackground', 'whiteBackground', 'whiteBackground', 'AeroBlastBackground', 'whiteBackground', 'whiteBackground', 'whiteBackground', 'whiteBackground', 'whiteBackground', 'whiteBackground', 'whiteBackground']

animationColor = [BLACK, WHITE, WHITE, WHITE, WHITE, WHITE, BLACK, WHITE, WHITE, WHITE, WHITE, WHITE, WHITE, BLACK]

attackImage = pygame.image.load('../images/attackAnimations/' + animation[ATTACK] + '.bmp').convert()
attackBackground = pygame.image.load('../images/attackAnimations/' + animationBackground[ATTACK] + '.png')

backgroundImage = pygame.image.load("../images/BattleBackgrounds/background.png")
secondBackgroundImage = pygame.image.load("../images/BattleBackgrounds/sprite5.png")

attackSprite = []
xyflippedAttack = []
xflippedAttack = []
yflippedAttack = []

playerAttack = False
opponentAttack = True

playerPokemon = 6
opponentPokemon = 6

pokeFrontImage = pygame.image.load('../images/fronts/' + str(opponentPokemon) + '.png')#
topPokePos = [192 - (pokeFrontImage.get_size()[0] / 2 ), 95 - (pokeFrontImage.get_size()[1])]
       
pokeBackImage = pygame.image.load('../images/back/' + str(playerPokemon) + '.png')
bottomPokePos = [64 - (pokeBackImage.get_size()[0] / 2), battleScreenHeight - (pokeBackImage.get_size()[1] / 1.5)]

for ROW in range(animationRow[ATTACK]):
    for COLLUMN in range(animationCollumn[ATTACK]):
        nextFrame = pygame.Surface((animationWidth[ATTACK], animationHeightv2[ATTACK]))
        nextFrame.blit(attackImage, (0,0), (COLLUMN * animationWidth[ATTACK], ROW * animationHeight[ATTACK], COLLUMN * animationWidth[ATTACK] + (animationWidth[ATTACK] - 1), ROW * animationHeight[ATTACK] + (animationHeight[ATTACK] - 1)))
        nextFrame.set_colorkey(animationColor[ATTACK])
        attackSprite.append(nextFrame)
        
        xyflippedNextFrame = pygame.transform.flip(nextFrame, True, True)
        xyflippedAttack.append(xyflippedNextFrame)
        
        xflippedNextFrame = pygame.transform.flip(nextFrame, True, False)
        xflippedAttack.append(xflippedNextFrame)
        
        yflippedNextFrame = pygame.transform.flip(nextFrame, False, True)
        yflippedAttack.append(yflippedNextFrame)
        
        if len(attackSprite) == animationFrames[ATTACK]:
            break
    if len(attackSprite) == animationFrames[ATTACK]:
        break
    
frame = 0
clock= pygame.time.Clock()
delay = 0
attacking = True
while True:
    for i in pygame.event.get():
        if i.type == pygame.QUIT:
            pygame.quit()
            quit()
    pygame.transform.scale2x(battlescreen, screen)
    battlescreen.blit(backgroundImage, (0,0))
    battlescreen.blit(secondBackgroundImage, (0, 0))
    battlescreen.blit(pokeFrontImage, (topPokePos[0], topPokePos[1]))
    battlescreen.blit(pokeBackImage, (bottomPokePos[0], bottomPokePos[1]))
    if attacking == True:
        if playerAttack == True:
            if ATTACK == 0 or ATTACK == 1:
                battlescreen.blit(attackBackground, (0, 0))
                battlescreen.blit(pokeFrontImage, (topPokePos[0], topPokePos[1]))
                battlescreen.blit(pokeBackImage, (bottomPokePos[0], bottomPokePos[1]))
                battlescreen.blit(attackSprite[frame], (0, 0))
            elif ATTACK == 6:
                print('WHICH WAY DOES THE ATTACK GO?????????????????')
            elif ATTACK == 7:
                battlescreen.blit(xyflippedAttack[frame], (-1, 40))
            elif ATTACK == 11:
                battlescreen.blit(attackSprite[frame], (2, 0))
            elif ATTACK == 12:
                battlescreen.blit(xflippedAttack[frame], (-2, -40))
            elif ATTACK == 13:
                battlescreen.blit(attackSprite[frame], [(192 - (attackSprite[frame].get_size()[0] / 2 )), (80 - (attackSprite[frame].get_size()[1]))])
                attackSprite[frame] = pygame.transform.scale2x(attackSprite[frame])
            else:
                battlescreen.blit(attackSprite[frame], (0,0))
    
        elif opponentAttack == True:
            if ATTACK == 0:
                attackBackgroundFlipped = pygame.transform.flip(attackBackground, True, True)
                battlescreen.blit(attackBackgroundFlipped, (0, 0))
                battlescreen.blit(pokeFrontImage, (topPokePos[0], topPokePos[1]))
                battlescreen.blit(pokeBackImage, (bottomPokePos[0], bottomPokePos[1]))
                battlescreen.blit(xflippedAttack[frame], (0, 20))
            elif ATTACK == 1:
                battlescreen.blit(attackBackground, (0, 0))
                battlescreen.blit(pokeFrontImage, (topPokePos[0], topPokePos[1])) # BLIT OPPONENTS POKE AFTER THE ATTACK ANIMATION
                battlescreen.blit(pokeBackImage, (bottomPokePos[0], bottomPokePos[1]))
                battlescreen.blit(xyflippedAttack[frame], (0, 20))
            elif ATTACK == 2:
                battlescreen.blit(xyflippedAttack[frame], (-2, 40))
            elif ATTACK == 3:
                battlescreen.blit(xyflippedAttack[frame], (-1, -1))
            elif ATTACK == 4:
                battlescreen.blit(xflippedAttack[frame], (0, 1)) ########## THIS ANIMATION NEEDS A LOT OF WORK, I.E. A NEW ONE
            elif ATTACK == 5:
                battlescreen.blit(xyflippedAttack[frame], (-1, 40))
            elif ATTACK == 6:
                battlescreen.blit(attackSprite[frame], (-40, 20))  #  FIX THE BACKGROUND SHIT WITH THIS IMAGE, I.E. REDO IT W/ GIMP
            elif ATTACK == 7:
                battlescreen.blit(attackSprite[frame], (10, 0))
            elif ATTACK == 8:
                battlescreen.blit(xyflippedAttack[frame], (-3, 40))
            elif ATTACK == 9:######################################
                frame = len(attackSprite) - 1
                battlescreen.blit(xyflippedAttack[frame], (0, 0))
            elif ATTACK == 10:
                battlescreen.blit(xflippedAttack[frame], (-3, -40))   # BLIT OPPONENTS POKEMON AFTER THE ANIMATION FRAME
            elif ATTACK == 11:
                battlescreen.blit(xflippedAttack[frame], (-2, 50))
            elif ATTACK == 12:
                battlescreen.blit(attackSprite[frame], (0, 0))
            elif ATTACK == 13:
                battlescreen.blit(attackSprite[frame], (75 - (pokeBackImage.get_size()[0] / 2), 140 - (pokeBackImage.get_size()[1] / 1.5)))
                attackSprite[frame] = pygame.transform.scale2x(attackSprite[frame])
            
    pygame.display.flip()
    
    if ATTACK == 9 and opponentAttack == True:
        frame -= 2
    
    if delay == 0:
        frame += 1
    delay += 1
    delay %= 2
    if ATTACK == 2:
        clock.tick(20)
    elif ATTACK == 14:
        clock.tick(8)
    elif ATTACK == 11:
        clock.tick(10)
    elif ATTACK == 13:
        clock.tick(8)
    else:
        clock.tick(15)
    if frame == animationFrames[ATTACK]:
        attacking = False
        if playerAttack == True and ATTACK == 9:
            pygame.time.delay(400)
        if ATTACK == 0:
            pygame.time.delay(1000)
    frame %= animationFrames[ATTACK]
    
    








